import requests
from lxml import etree
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.append(["工作名称","工作地址","发布时间","工资","工作经验","需要的技能","公司名称"])
headers ={
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
}
url = "https://www.lagou.com/wn/jobs?px=new&pn={}&kd={}&city={}"

your_work = input("请输入你要查询的工作：")
city = input("请输入求职地点：")

new_url = url.format(1,your_work,city)
resp = requests.get(new_url,headers=headers)
html = etree.HTML(resp.text)
city_list = html.xpath('//div[@class="hot-city__1Sprg"]/div/text() | //div[@class="hot-city__1Sprg"]/div//span/text()')
judge = False
if city == '':
    city="全国"
else:
    for i in city_list:
        a = str(i)
        if a == city:
            judge = True
            break
if judge:
    page = int(html.xpath('//div[@class="pagination__1L2PP"]/ul/li/@title')[-2])
    for i in range(page+1):
        new_url = url.format(i, your_work, city)
        resp = requests.get(new_url, headers=headers)
        html = etree.HTML(resp.text)
        all_list = html.xpath('//div[@class="list__YibNq"]/div')
        # print(len(title_list))
        for t in all_list:
            title = t.xpath('.//div[@class="p-top__1F7CL"]/a/text()')
            # 工作名称
            work = title[0]
            # 工作地址
            address = title[1]
            # 发布时间
            time = t.xpath('.//div[@class="p-top__1F7CL"]/span/text()')
            time = time[0]
            # 工资
            money = t.xpath('.//div[@class="p-bom__JlNur"]/span/text()')[0]
            # 经验
            experience = t.xpath('.//div[@class="p-bom__JlNur"]/text()')[0]
            # 需要的技能
            label = t.xpath('.//div[@class="ir___QwEG"]/span/text()')
            all_label = ''
            for i in label:
                all_label = all_label + i + "、"
            # 公司名称
            company = t.xpath('.//div[@class="company-name__2-SjF"]/a/text()')[0]
            ws.append([work, address, time, money, experience, all_label, company])
    wb.save("data/{}工作汇总.xlsx".format(your_work))
else:
    print("你输入的城市“{}”不正确".format(city))


